
***************************************************
*Table A.4
***************************************************
use "$workdata/baseline", clear /* see $data_do/baseline.do */

g e=1
g b_month=month(foed_dag)
g b_year=year(foed_dag)

collapse wage sa (sum) e, by(pnr age_q year month b_month b_year) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g age_q0_4= age_q0^4
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3
g dXage_q0_4=d*age_q0_4

*********************************************************************

***************************************************
*Table A.4, Baseline
***************************************************

eststo clear
local model=0
foreach y of varlist sa wage {
preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

preserve
keep if age_q>=21 & age_q<29


sum `y' if age_month_q==24.11
local initial=round(r(mean), 0.01)
di `initial'


reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res


forvalues s=5/5 {

local spec_num=`s'
if `y'==wage local title="Employment (residuals)"
if `y'==sa   local title="Social Assistance (residuals)"
if `y'==wage local fig_num=6
if `y'==sa local fig_num=2

if `spec_num'==1 local spec="d age_q0"
if `spec_num'==2 local spec="d age_q0 age_q0_2"
if `spec_num'==3 local spec="d age_q0 age_q0_2 age_q0_3"
if `spec_num'==4 local spec="d age_q0                   dXage_q0"
if `spec_num'==5 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"

if `spec_num'==1 local spec_name="1st order polynomial"
if `spec_num'==2 local spec_name="2nd order polynomial"
if `spec_num'==3 local spec_name="3rd order polynomial"
if `spec_num'==4 local spec_name="1st order polynomial spline"
if `spec_num'==5 local spec_name="2nd order polynomial spline"


local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec'  [aw=e] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
local pct_of_initial= round(((round(_b[d],0.001)/`initial')*100), 0.1)
di `pct_of_initial'

regsave d using $tables/tableA4_1 , addlabel(samplesize, `obs', N_clust,`N_clust',outcome,`y',spec,`spec_name',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'

}
restore
}

***************************************************
*Table A.4, Using all observations (a)
***************************************************

eststo clear
local model=0
foreach y of varlist sa wage {
preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

preserve
keep if age_q>=21 & age_q<29


sum `y' if age_month_q==24.11
local initial=round(r(mean), 0.01)
di `initial'


reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res


forvalues s=5/5 {

local spec_num=`s'
if `y'==wage local title="Employment (residuals)"
if `y'==sa   local title="Social Assistance (residuals)"
if `y'==wage local fig_num=6
if `y'==sa local fig_num=2

if `spec_num'==1 local spec="d age_q0"
if `spec_num'==2 local spec="d age_q0 age_q0_2"
if `spec_num'==3 local spec="d age_q0 age_q0_2 age_q0_3"
if `spec_num'==4 local spec="d age_q0                   dXage_q0"
if `spec_num'==5 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"

if `spec_num'==1 local spec_name="1st order polynomial"
if `spec_num'==2 local spec_name="2nd order polynomial"
if `spec_num'==3 local spec_name="3rd order polynomial"
if `spec_num'==4 local spec_name="1st order polynomial spline"
if `spec_num'==5 local spec_name="2nd order polynomial spline"


local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec'  [aw=e] /*if resp==0*/ , vce(cluster pnr)
local N_clust=e(N_clust)
local pct_of_initial= round(((round(_b[d],0.001)/`initial')*100), 0.1)
di `pct_of_initial'

regsave d using $tables/tableA4_2 , addlabel(samplesize, `obs', N_clust,`N_clust',outcome,`y',spec,`spec_name',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'

}
restore
}


***************************************************
*Table A.4, Using all observations (b)
***************************************************

eststo clear
local model=0
foreach y of varlist wage sa {
forvalues bw=1/1 {

preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

forvalues s=1/1{
preserve
local age_min=25-`bw'
local age_max=25+`bw'
keep if age_q>=21 & age_q<29

sum `y' if age_month_q==24.11
local initial=round(r(mean), 0.01)
di `initial'

reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res

local bw_num=`bw'
if `y'==wage local title="Employment"
if `y'==sa   local title="Social Assistance"

if `s'==1 local spec="d age_q0                   dXage_q0"
if `s'==2 local spec="d age_q0"
if `s'==1 local spec_name="Linear spline"
if `s'==2 local spec_name="Linear"
if `bw'==1 local bw_name="+/- `bw' year"
if `bw'!=1 local bw_name="+/- `bw' years"

g w=max(0,`bw'-abs(age_q0))
g w2=w*e
local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec' [aw=w2] /*if resp==0*/ , vce(cluster pnr)
local N_clust=e(N_clust)
local pct_of_initial= round(((round(_b[d],0.001)/`initial')*100), 0.1)
di `pct_of_initial'

regsave d using $tables/tableA4_3,  addlabel(samplesize,`obs',N_clust,`N_clust',outcome,`y',bw,`bw_name',spec,`spec_name',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'


restore
}
}
}

***************************************************
*Table A.4, Cells in semester rather than months (a)
***************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */

capture drop age_q
g age_q=floor( (age-floor(age))*2)*(1/2) + floor(age)
g e=1
g b_month=month(foed_dag)
g b_year=year(foed_dag)

collapse wage sa (sum) e, by(pnr age_q year month b_month b_year) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=26 & age_month_q<=25"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************


eststo clear
local model=0
foreach y of varlist wage sa {

preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

forvalues s=2/2  {
preserve
keep if age_q>=21 & age_q<29

if `y'==wage local initial=0.55
if `y'==sa   local initial=0.13
di `initial'

reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res

local spec_num=`s'
if `y'==wage local title="Employment"
if `y'==sa   local title="Social Assistance"
local min_wage=-0.05
local max_wage=0.05
local min_sa  =-0.05
local max_sa  =0.05
if `spec_num'==2 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"
if `spec_num'==2 local spec_name="2nd order polynomial spline"
local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec' [aw=e] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
regsave d using $tables/tableA4_4,  addlabel(samplesize,`obs',N_clust,`N_clust',outcome,`y',bw,`bw_name',spec,`spec_name',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'

restore
}
}

***************************************************
* end: Table A.4
***************************************************
